package com.stone.upms.upms.controller;

import com.stone.upms.upms.commons.AjaxResult;
import com.stone.upms.upms.commons.PageInfoResult;
import com.stone.upms.upms.domain.ApplicationSystem;
import com.stone.upms.upms.dto.ApplicationSystemDto;
import com.stone.upms.upms.service.ApplicationSystemService;
import com.stone.upms.upms.utils.UserUtil;
import io.swagger.annotations.*;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.data.domain.Page;
import org.springframework.http.MediaType;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/api/application")
@Api(value = "Application", description = "应用系统Controller", tags = "Application")
public class ApplicationSystemController {
    @Autowired
    private ApplicationSystemService service;

    @PostMapping(value = "/addOrEdit")
    @ApiOperation(value = "添加或编辑系统信息", notes = "添加或编辑系统信息，id值为0或空新增系统信息，否则更新新系统信息。", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiResponses({@ApiResponse(code = 200, message = "JSON对象，retcode属性值为1，则添加成功，否则后台报错，通过retmsg查看报错原因。")})
    public AjaxResult saveApplicationSystem(ApplicationSystemDto dto) {
        ApplicationSystem applicationSystem = new ApplicationSystem();
        BeanUtils.copyProperties(dto, applicationSystem);
        if (null != dto.getId() && dto.getId() > 0) {
            applicationSystem.setCreateBy(UserUtil.getCurrentUserName());
            applicationSystem.setCreateOn(new Date());
        } else {
            applicationSystem.setUpdateBy(UserUtil.getCurrentUserName());
            applicationSystem.setUpdateOn(new Date());
        }
        service.saveObj(applicationSystem);
        return new AjaxResult();
    }

    @GetMapping("/list")
    @ApiOperation(value = "应用系统列表", notes = "获取应用系统列表。", httpMethod = "GET", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiImplicitParams({@ApiImplicitParam(paramType = "query", name = "name", dataType = "string", value = "应用系统名称"),
            @ApiImplicitParam(paramType = "query", name = "onOff", dataType = "string", value = "启用/禁用"),
            @ApiImplicitParam(paramType = "query", name = "pageNumber", dataType = "long", value = "页码"),
            @ApiImplicitParam(paramType = "query", name = "pageSize", dataType = "long", value = "单页条数")})
    public PageInfoResult<ApplicationSystem> getList(@RequestParam(required = false) String name, @RequestParam(required = false) String onOff, @RequestParam(required = false) Integer pageNumber, @RequestParam(required = false) Integer pageSize) {
        ApplicationSystem applicationSystem = new ApplicationSystem();
        applicationSystem.setName(name);
        applicationSystem.setOnOff(onOff);
        Page<ApplicationSystem> pageList = service.findAll(applicationSystem, pageNumber, pageSize);
        return new PageInfoResult<>(pageList.getTotalElements(), pageList.getContent());
    }

    @InitBinder
    protected void init(HttpServletRequest request, ServletRequestDataBinder binder) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        dateFormat.setLenient(false);
        binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, false));
    }
}
